package com.itheima.service.impl;

import com.itheima.dao.SysDictDataDao;
import com.itheima.pojo.SysDictData;
import com.itheima.service.SysDictDataService;
import com.itheima.vo.SysDictDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysDictDataServiceImpl implements SysDictDataService {

    @Autowired
    private SysDictDataDao sysDictDataDao;

    /**
     * 添加字典数据
     *@param sysDictDataVO
     * @return void
     *@author itheima
     */
    @Override
    public void add(SysDictDataVO sysDictDataVO) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setCssClass(sysDictDataVO.getCssClass());
        sysDictData.setDictLabel(sysDictDataVO.getDictLabel());
        sysDictData.setDictValue(sysDictDataVO.getDictValue());
        sysDictData.setIsDefault(String.valueOf(sysDictDataVO.getIsDefault()));
        sysDictData.setIsPublish(sysDictDataVO.getIsPublish());
        sysDictData.setListClass(sysDictDataVO.getListClass());
        sysDictData.setOid(sysDictDataVO.getOid());
        sysDictData.setRemark(sysDictDataVO.getRemark());
        sysDictData.setSort(sysDictDataVO.getSort());
        sysDictData.setStatus(String.valueOf(sysDictDataVO.getStatus()));
        sysDictData.setUid(sysDictDataVO.getUid());
        sysDictData.setCreateByUid("1");
        sysDictData.setUpdateByUid("1");

        sysDictDataDao.insertSelective(sysDictData);
    }

    /**
     * 批量删除字典数据
     *@param sysDictDataVOList
     * @return void
     *@author itheima
     */
    @Override
    public void deleteBatch(List<SysDictDataVO> sysDictDataVOList) {
        SysDictData sysDictData = new SysDictData();
        if (sysDictDataVOList != null || sysDictDataVOList.size() > 0) {
            for (SysDictDataVO sysDictDataVO : sysDictDataVOList) {
                if (sysDictDataVO.getUid() != null || "".equals(sysDictDataVO.getUid())) {
                    sysDictData.setUid(sysDictDataVO.getUid());
                    sysDictDataDao.deleteByPrimaryKey(sysDictData.getUid());
                }
            }
        }
    }

    /**
     * 编辑字典数据
     *@param sysDictDataVO
     * @return void
     *@author itheima
     */
    @Override
    public void edit(SysDictDataVO sysDictDataVO) {
        SysDictData sysDictData = new SysDictData();

        sysDictData.setCssClass(sysDictDataVO.getCssClass());
        sysDictData.setDictLabel(sysDictDataVO.getDictLabel());
        sysDictData.setDictValue(sysDictDataVO.getDictValue());
        sysDictData.setIsDefault(String.valueOf(sysDictDataVO.getIsDefault()));
        sysDictData.setIsPublish(sysDictDataVO.getIsPublish());
        sysDictData.setListClass(sysDictDataVO.getListClass());
        sysDictData.setOid(sysDictDataVO.getOid());
        sysDictData.setRemark(sysDictDataVO.getRemark());
        sysDictData.setSort(sysDictDataVO.getSort());
        sysDictData.setStatus(String.valueOf(sysDictDataVO.getStatus()));
        sysDictData.setUid(sysDictDataVO.getUid());

        sysDictDataDao.updateByPrimaryKeySelective(sysDictData);
    }

    /**
     * 获取字典数据列表
     *@param sysDictDataVO
     * @return java.util.List<com.itheima.pojo.SysDictData>
     *@author itheima
     */
    @Override
    public List<SysDictData> getList(SysDictDataVO sysDictDataVO) {
        SysDictData sysDictData = new SysDictData();
        List<SysDictData> sysDictDataList = sysDictDataDao.selectByExample(new Example(SysDictData.class));
        return sysDictDataList;
    }

    /**
     * 根据字典类型获取字典数据
     *@param dictType
     * @return java.util.List<com.itheima.pojo.SysDictData>
     *@author itheima
     */
    @Override
    public List<SysDictData> getListByDictType(String dictType) {
        if (dictType != null || !"".equals(dictType)) {
            String uid = sysDictDataDao.selectDictTypeUidByDictType(dictType);
            List<SysDictData> sysDictDataList = sysDictDataDao.getListByDictType(uid);
            return sysDictDataList;
        } else {
            throw new  RuntimeException("输入的值有误");
        }
    }

    /**
     * 根据字典类型数组获取字典数据
     *@param dictTypeList
     * @return java.util.List<com.itheima.pojo.SysDictData>
     *@author itheima
     */
    @Override
    public List<SysDictData> getListByDictTypeList(List<String> dictTypeList) {
        List<SysDictData> listByDictType = null;
        if (dictTypeList != null || dictTypeList.size() > 0) {
            for (String dictType : dictTypeList) {
                listByDictType = this.getListByDictType(dictType);
            }
        } else {
            throw new RuntimeException("输入的数据格式有误");
        }
        return listByDictType;
    }
}
